﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyBank
{
    public class Bank
    {

        User user = new User(); // 当前选择的账号

        User[] _userGroup = new User[3];

        public void Initial()
        {
            User user0 = new User();
            user0.Name = "家美乐";
            user0.Account = "12345678";
            user0.Password = "2019";
            user0.IdentityNum = "2019";
            user0.Blance = 2019;

            _userGroup[0] = user0;


            _userGroup[1] = new User();
            _userGroup[1].Account = "179708064359";
            _userGroup[1].Name = "张颖颖";
            _userGroup[1].Password = "4321";
            _userGroup[1].IdentityNum = "510010619891231127";
            _userGroup[1].Blance = 2000;

            _userGroup[2] = new User();
            _userGroup[2].Account = "179708064368";
            _userGroup[2].Name = "刘华";
            _userGroup[2].Password = "4567";
            _userGroup[2].IdentityNum = "410207198904051271";
            _userGroup[2].Blance = 8000;


        }

        public void ShowAllUser()
        {
            foreach (User u in _userGroup)
            {
                Console.WriteLine("帐户姓名:" + u.Name + " 帐号：" + u.Account + " 存款余额：" + u.Blance + " 密码：" + u.Password + " 身份证号：" + u.IdentityNum+"\n");
            }
        }

        public void CreateBank()
        {
            

            Console.WriteLine("请输入姓名：");
            user.Name = Console.ReadLine();

            user.Account = "123456789";

            Console.WriteLine("请输入密码：");
            user.Password = Console.ReadLine();
            Console.WriteLine("请输入身份证号：");
            user.IdentityNum = Console.ReadLine();
            Console.WriteLine("请输入存款金额：");
            user.Blance = double.Parse(Console.ReadLine());

            Console.WriteLine("账户：{0}，用户名是：{1}，存款金额：{2} 创建成功",user.Account,user.Name,user.Blance);

            Console.ReadLine();
        }

        public void ShowMenu()
        {
            Console.WriteLine("欢迎使用自助银行服务");
            Console.WriteLine("1.存款 2.取款 3.转账");


        }

        #region 取款
        /// <summary>
        /// 取款
        /// </summary>
        public void WithDraw()
        {
            string account = "";             //帐号
            string pwd;                      //密码

            Console.WriteLine("请输入帐号:");
            account = Console.ReadLine();
            if (account.Length == 0)
            {
                Console.WriteLine("输入的帐号不正确！");
                return;
            }

            //接收帐户密码，并验证
            Console.WriteLine("请输入帐户密码:");
            pwd = Console.ReadLine();

            User user = CheckUser(account, pwd);
            if (user == null)
            {
                Console.WriteLine("账号或密码错误");
                return;
            }

            Console.WriteLine("请输入取款金额");
            double money = Convert.ToDouble(Console.ReadLine()); // double.Parse(Console.ReadLine());

            double result = user.MinusMoney(money);
            if (result == -1)
            {
                Console.WriteLine("取款失败");
            }
            else
            {
                Console.WriteLine("取款成功!当前余额:" + result);
            }
        }
        #endregion

        public void Deposit()
        {
            Console.WriteLine("请输入帐号:");
            string account = Console.ReadLine();
            if (account.Length == 0)
            {
                Console.WriteLine("请输入帐号！");
                return;
            }

            // 验证账号是否存在

            user = CheckUserByAccount(account);

            if (user == null)
	        {
		        Console.WriteLine("输入的帐号不正确！");
                return;
	        }

            Console.WriteLine("请输入存款金额:");
            double money = double.Parse(Console.ReadLine());

            double result = user.SaveMoney(money);

            if (result == -1)
            {
                Console.WriteLine("操作失败");
            }
            else
            {
                Console.WriteLine("操作成功，当前余额：{0}", result);
            }

            

        }

        /// <summary>
        /// 检查账户是否存在
        /// </summary>
        /// <returns>账户对象，不存在就返回null</returns>
        public User CheckUser(string account,string pwd) 
        {
            foreach (User item in _userGroup)
            {
                if (item.Account.Equals(account) && item.Password.Equals(pwd))
                {
                    return item;
                }
            }

            return null;
        }

        /// <summary>
        /// 检查账户是否存在
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public User CheckUserByAccount(string account)
        {
            foreach (User item in _userGroup)
            {
                if (item.Account.Equals(account))
                {
                    return item;
                }
            }

            return null;

        }
        /// <summary>
        /// 转账
        /// </summary>
        /// <param name="fAccount"></param>
        /// <param name="fPwd"></param>
        /// <param name="tAccount"></param>
        /// <param name="money"></param>
        /// <param name="fBalance"></param>
        /// <param name="tBalance"></param>
        /// <returns></returns>
        private int Transfer(string fAccount,string fPwd,string tAccount,double money,ref double fBalance,ref double tBalance)
        {
            //检查转出账号和密码
            User userFrom = CheckUser(fAccount, fPwd);

            if (userFrom == null)
            {
                return -1; //转出账号或密码不正确
            }

            //检查转入账号
            User userTo = CheckUserByAccount(tAccount);

            if (userTo == null)
            {
                return -2; // 转入账号不正确
            }

            double resultFrom = userFrom.MinusMoney(money);

            if (resultFrom == -1)
            {
                return -3; //转账操作失败
            }

            double resultTo = userTo.SaveMoney(money);

            if (resultTo == -1)
            {
                return -3; //转账操作失败
            }

            tBalance = userTo.Blance;
            fBalance = userFrom.Blance;

            return 1; //转账成功
        }

        public void InputTransferData()
        {
            Console.WriteLine("请输入转出帐号：");
            string fromAccount = Console.ReadLine();
            Console.WriteLine("请输入转出帐户密码：");
            string fromPwd = Console.ReadLine();
            Console.WriteLine("请输入转入帐号：");
            string toAccount = Console.ReadLine();
            Console.WriteLine("请输入转帐金额：");
            double money = double.Parse(Console.ReadLine());


            double fbalance = 0; //转出账户的余额
            double tbalance = 0; //转入账号的余额

            int iRet = Transfer(fromAccount, fromPwd, toAccount, money, ref fbalance, ref tbalance);

            if (iRet == 1)
            {
                Console.WriteLine("转帐成功，转出帐号{0}的余额为：{1}，转入帐号{2}的余额为：{3}", fromAccount, fbalance, toAccount, tbalance);
            }
            else if (iRet == -1)
            {
                Console.WriteLine("转出帐户的帐号或密码输入错误！");
            }
            else if (iRet == -2)
            {
                Console.WriteLine("转入帐号不正确！");
            }
            else if (iRet == -3)
            {
                Console.WriteLine("转帐操作失败！");
            }
        }

        public void ChangePwd()
        {
            string account = string.Empty;
            string pwd = string.Empty;

            Console.WriteLine("请输入账号：");
            account = Console.ReadLine();
            Console.WriteLine("请输入原密码：");
            pwd = Console.ReadLine();

            User user = CheckUser(account, pwd);
            //检查用户或密码是否正确
            if (user == null)
            {
                Console.WriteLine("账号或密码不正确");
                return;
            }

            Console.WriteLine("请输入新密码：");
            string newPwd = Console.ReadLine();

            Console.WriteLine("请再次输入新密码：");
            string rePwd = Console.ReadLine();

            if (newPwd.Equals(rePwd))
            {
                user.Password = newPwd;
                Console.WriteLine("密码修改成功");
            }

        }

    }
}
